package io.wdb.modules.generator.generator.controller.book;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.wdb.common.annotation.SysLog;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.generator.generator.controller.api.BaseController;
import io.wdb.modules.generator.generator.controller.operation.BookBorrowOperationController;
import io.wdb.modules.generator.generator.entity.BookTagMappingEntity;
import io.wdb.modules.generator.generator.entity.web.BaseResponse;
import io.wdb.modules.generator.generator.entity.web.WebResponse;
import io.wdb.modules.generator.generator.service.book.BookLabelService;

@RestController
@RequestMapping("book/label")
public class BookLabelController extends BaseController{
	private Logger logger = LoggerFactory.getLogger(BookBorrowOperationController.class);
	@Autowired
	private BookLabelService bookLabelService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	//@RequiresPermissions("accountcash:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        List<BookTagMappingEntity> bookTopicalMappingsEntities = bookLabelService.queryList(query);
        
		int total = bookLabelService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(bookTopicalMappingsEntities, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	/**
	 * 
	 * @Method:saveOrUpdateLabel
	 * @Description:新增或编辑标签
	 * @Time 2017年12月24日 下午3:55:14
	 * @author: Tom
	 * @param bookTagMappingEntity
	 * @return
	 */
	@SysLog("新增或编辑标签")
	@RequestMapping("/saveOrUpdateLabel")
	public BaseResponse saveOrUpdateLabel(@RequestBody BookTagMappingEntity bookTagMappingEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = new WebResponse();
			if(bookTagMappingEntity.getId()==null) {
				webResponse = bookLabelService.insertLabel(bookTagMappingEntity);
			}else {
				webResponse = bookLabelService.updateLabel(bookTagMappingEntity);
			}
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse=failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
	/**
	 * 
	 * @Method:deleteLable
	 * @Description:删除标签
	 * @Time 2017年12月24日 下午3:59:45
	 * @author: Tom
	 * @param bookTagMappingEntity
	 * @return
	 */
	@SysLog("删除标签")
	@RequestMapping("/deleteLabel")
	public BaseResponse deleteLabel(@RequestBody BookTagMappingEntity bookTagMappingEntity){
		BaseResponse baseResponse = success();
		try {
			WebResponse webResponse = new WebResponse();
			webResponse = bookLabelService.deleteLable(bookTagMappingEntity.getId());
			baseResponse.setDetail(webResponse);
		} catch (Exception e) {
			baseResponse=failed(e);
			logger.error(e.getMessage());
		}
		return baseResponse;
	}
}
